package com.wy.xinzhi.common.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.dao.IDataBaseDao;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.services.AbstractBaseService;
import com.wy.xinzhi.common.pojos.THoliday;

/**
 * 节假日管理
 * 
 * @author X
 * 
 */
public class HolidayService extends AbstractBaseService {

	private static Logger logger = LoggerFactory
			.getLogger(HolidayService.class);
	static PropertiesUtil p = new PropertiesUtil();

	private JdbcTemplate jdbcTemplate;
	private HibernateDaoSupport hibernateDao;
	private IDataBaseDao dataBaseDao;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public HibernateDaoSupport getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(HibernateDaoSupport hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IDataBaseDao getDataBaseDao() {
		return dataBaseDao;
	}

	public void setDataBaseDao(IDataBaseDao dataBaseDao) {
		this.dataBaseDao = dataBaseDao;
	}

	/**
	 * 获取节假日信息
	 * 
	 * @param year
	 * @return
	 */
	public List<Map<String, Object>> queryHolidays(String year) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		StringBuffer sql = new StringBuffer(
				"select * from t_holiday where YEAR(holi_Date)='");
		sql.append(year);
		sql.append("'");
		list = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		logger.debug("查询年份" + year + "的节假日，详细：" + sql.toString());
		return list;
	}

	/**
	 * 新增节假日信息
	 * 
	 * @param obj
	 * @return
	 */
	public THoliday addHoliday(THoliday obj) throws ServiceException {
		THoliday holiday = obj;
		try {
			hibernateDao.getHibernateTemplate().save(obj);
		} catch (DataAccessException e) {
			logger.error("系统错误:{}", e);
			throw new ServiceException(e);
		}
		return holiday;
	}

	/**
	 * 修改节假日信息
	 * 
	 * @param obj
	 * @return
	 */
	public THoliday updateHoliday(THoliday obj) throws ServiceException {
		THoliday holiday = obj;
		try {
			hibernateDao.getHibernateTemplate().update(obj);
		} catch (DataAccessException e) {
			logger.error("系统错误:{}", e);
			throw new ServiceException(e);
		}
		return holiday;
	}

	/**
	 * 删除节假日信息
	 * 
	 * @param obj
	 * @return
	 */
	public boolean deleteHoliday(THoliday obj) throws ServiceException {
		boolean isSucc = true;
		try {
			hibernateDao.getHibernateTemplate().delete(obj);
		} catch (DataAccessException e) {
			logger.error("系统错误:{}", e);
			isSucc = false;
			throw new ServiceException(e);
		} finally {
			return isSucc;
		}
	}

	/**
	 * 判断是否节假日
	 * 
	 * @param day
	 * @return
	 */
	public boolean isHoliday(String day) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		boolean isHoliday = true;
		StringBuffer sql = new StringBuffer(
				"select * from t_holiday where holi_Date='");
		sql.append(day);
		sql.append("'");
		list = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		logger.debug("判断" + day + "是否节假日，详细：" + sql.toString());
		if (list != null && list.size() > 0) {
			isHoliday = false;
		} else {
			isHoliday = true;
		}
		return isHoliday;
	}
}
